//
//  YSHomeProdListView.m
//  Yueshi
//
//  Created by Northwan on 15/7/18.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//

#import "YSHomeProdListView.h"
#import "YSHomeCardHeaderView.h"
#import "YSHomeCardFooterView.h"
#import "YSProductList.h"
#import "YSHomeProdListCell.h"
#import "YSLoginCtrl.h"
#import "CustomNavigationCtrl.h"
#import "YSProductDetailCtrl.h"


@interface YSHomeProdListView()<UITableViewDataSource,UITableViewDelegate>
@property (strong,nonatomic) UITableView *tableView;
@property (strong,nonatomic) NSMutableArray *itemArray;
@property (strong,nonatomic) YSHomeCardHeaderView *headerView;
@property (strong,nonatomic) YSHomeCardFooterView *footerView;
@property (assign,nonatomic) CGFloat allCellHeight;
@property (assign,nonatomic) HomeSubViewSourceType sourceType;
@end

@implementation YSHomeProdListView

-(NSMutableArray *)itemArray{
    if(!_itemArray){
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

-(instancetype)initWithSourceType:(HomeSubViewSourceType)sourceType{
    self = [super init];
    if(self){
        self.sourceType = sourceType;
        [self setFrame:[UIScreen mainScreen].bounds];
        self.headerView = [YSHomeCardHeaderView headerView];
        self.footerView = [YSHomeCardFooterView footerView];
        [self initTableView];
        [self setBackgroundColor:PUBLIC_CARD_BG];
    }
    return self;
}

-(void)initTableView{
    self.tableView = [[UITableView alloc]init];
    [self.tableView setFrame:CGRectMake(0, 0, self.width, self.height)];
    [self.tableView setBackgroundColor:PUBLIC_CARD_BG];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    [self.tableView setTableHeaderView:self.headerView];
    [self.tableView setTableFooterView:self.footerView];
    [self.tableView setBackgroundColor:PUBLIC_BG];
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 8, 0, 0);
    self.tableView.scrollEnabled = NO;
    [self addSubview:self.tableView];
}

-(void)setInfo:(YSCardProdResult *)info{
    _info = info;
    [_itemArray addObjectsFromArray:info.prodlist];
    [self.tableView reloadData];
    if (ISBLANK(info.headResult.cardName)) {
        [self.tableView setTableHeaderView:nil];
    }else{
        _allCellHeight += CARD_HEADERVIEW_HEIGHT;
        [self.tableView setTableHeaderView:self.headerView];
        [self.headerView setInfo:info.headResult];
    }
    if(ISBLANK(info.headResult.cardDesc)){
        [self.tableView setTableFooterView:nil];
    }else{
        _allCellHeight += CARD_FOOTERVIEW_HEIGHT;
        [self.tableView setTableFooterView:self.footerView];
        [self.footerView setInfo:info.headResult];
    }
    
    _allCellHeight += _itemArray.count*YSHomeProdListCellHeight;
    //根据所有cell的高计算出allCellHeight并设置tableView的高和本身View的高
    [self.tableView setHeight:_allCellHeight];
    [self setHeight:self.tableView.height];
}

#pragma - mark UITableViewDataSource,UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.itemArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YSHomeProdListCell *cell =  [YSHomeProdListCell initWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setInfo:self.itemArray[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return YSHomeProdListCellHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YSProductDetailCtrl *ctrl = [[YSProductDetailCtrl alloc]initWithProdId:[self.itemArray[indexPath.row] prodId]];
    if(_sourceType == HomeSourceType){
        [self.superview.viewController.navigationController pushViewController:ctrl animated:YES];
    }else{
        [self.viewController.navigationController pushViewController:ctrl animated:YES];
    }
}

@end
